package boss.portal.exception;

import boss.portal.common.ResponseData;
import boss.portal.controller.BaseController;
import boss.portal.controller.JwtExceptionController;
import boss.portal.controller.UserController;
import boss.portal.result.Result;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.net.ConnectException;

/**
 * @ClassName GlobalExceptionHandler
 * @Description 全局处理自定义的业务异常
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public ResponseData handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public ResponseData handleDuplicateKeyException(DuplicateKeyException e){
        logger.error(e.getMessage(), e);
        return error("数据库中已存在该记录");
    }

    @ExceptionHandler(UsernameIsExitedException.class)
    @ResponseBody
    public ResponseData usernameIsExitedException(UsernameIsExitedException e){
        logger.error(e.getMessage(), e);
        return error("用户已经存在");
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseData handleException(Exception e){
        logger.error(e.getMessage(), e);
        return error(e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public ResponseData serviceException(ServiceException e){
        logger.error(e.getMessage(), e);
        return error(e.getMessage());
    }

    @ExceptionHandler(ConnectException.class)
    @ResponseBody
    public ResponseData connectException(ConnectException e){
        logger.error(e.getMessage(), e);
        return error("系统调用异常");
    }

    @ExceptionHandler(ResourceAccessException.class)
    public ResponseData connectException(ResourceAccessException e){
        logger.error(e.getMessage(), e);
        return error("系统之间调用异常");
    }

    @ResponseBody
    @ExceptionHandler(value = {ExpiredJwtException.class})
    public ResponseData expiredJwtException(ExpiredJwtException e) {
        logger.error(e.getMessage(), e);
        return error("Token过期");
    }

    @ExceptionHandler(value = UnsupportedJwtException.class)
    @ResponseBody
    public ResponseData unsupportedJwtException(UnsupportedJwtException e) {
        logger.error(e.getMessage(), e);
        return error("Token签名失败");
    }

    @ExceptionHandler(value = SignatureException.class)
    @ResponseBody
    public ResponseData signatureException(SignatureException e) {
        logger.error(e.getMessage(), e);
        return error("Token格式错误");
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public ResponseData illegalArgumentException(IllegalArgumentException e) {
        logger.error(e.getMessage(), e);
        return error("Token非法参数异常");
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseBody
    public ResponseData accessDeniedException(AccessDeniedException e) {
        logger.error(e.getMessage(), e);
        return error("Token非法参数异常");
    }

    @ExceptionHandler(value = MalformedJwtException.class)
    @ResponseBody
    public ResponseData malformedJwtException(MalformedJwtException e) {
        logger.error(e.getMessage(), e);
        return error("Token没有被正确构造");
    }
}
